home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15593 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer to read file...
  5. Date: 19 Apr 1996 22:28 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <19APR199622282058@erich.triumf.ca>
  9. References: <1996Apr19.110223@bbs.ug.eds.com>
  10. NNTP-Posting-Host: ftp.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <1996Apr19.110223@bbs.ug.eds.com>, berryb@bbs.ug.eds.com (High Plains GRIPster (TRW C.S.D.)) writes...
  14. >4/19/96
  15. >I have a beginners C question.
  16. >After going page by page through my Learning C book,
  17. >I am still in a quandry.
  18. >I have a pointer to a file:    FILE *fp;
  19. >I have a pointer to a pointer:    FILE **p2f = &fp;
  20. >I have a character pointer:    char*fname="/user2/bcb/grip/unix/c/listing";
  21. >After I open my file...        if ( (fp = fopen (file1, "r")) != NULL)
  22.  
  23. I hope you really mean
  24.     if((fp = fopen(fname, "r")) != NULL)
  25. here??  (otherwise, what is "file1"?)
  26.  
  27. >I want to be able to use the p2f pointer, which is pointing at
  28. >the beginning of the file, to read the character that it is
  29. >pointing at...and then move to the next character and read
  30. >that character until EOF is reached.
  31.  
  32. a FILE *, in spite of it's name, does not point _at_ or _into_ the file.  It
  33. points to some system specific structure describing the file, and where or how
  34. to find it's contents - we don't need to know what's in this structure, but the
  35. file access functions provided with the compiler do know.
  36.  
  37. >I am able to increment the pointers position by  p2f++;
  38. >but I am unable to read the character.  This is probably
  39. >because I need a character pointer to read a character,
  40. >and the FILE pointer just cant perform that way.
  41.  
  42. correct.  You don't need p2f at all.
  43.  
  44. You can read a line from the file thus:
  45.  
  46.     char buffer[BIG_ENUF];
  47.     fgets(buffer, BIG_ENUF, fp);
  48.  
  49. or you can use fgetc() or getc() to get a single char.
  50.  
  51. After fgets() gets a line (or as much will fit in the buffer) or gets() or
  52. fgets() get a char, a "pointer into the file" is positioned ready to read the
  53. next char, or bunch of chars.  If you are just reading or writing a file
  54. sequentially, you don't need to worry about this "pointer  into  the file" - it
  55. will be automagically incremented as needed by the file access functions.
  56.  
  57. You can position this "pointer into the file" using fseek(), ftell(), and
  58. related functions - consult your compiler's function library reference.
  59.  
  60. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  61. Internet: bennett@triumf.ca         | of one another only when one can be
  62. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  63. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  64. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  65. or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
  66. or: http:://vancouver-webpages.com/peter/index.html
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.